package masterjava.common.util;

import masterjava.common.logging.MasterLogger;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

/**
 * User: GKislin
 * Date: 22.01.11
 */
public class Util {
    protected static final MasterLogger LOGGER = MasterLogger.get(Util.class);

    public static Map<?, ?> asMap(Object... parameters) {
        Map<Object, Object> result = new HashMap<Object, Object>();
        for (int i = 0; i < parameters.length; i = i + 2) {
            result.put(parameters[i], parameters[i + 1]);
        }
        return result;
    }

    public static <E extends Enum> Map<E, ?> asEnumMap(Class<E> clazz, Object... parameters) {
        Map<E, Object> result = new EnumMap(clazz);
        for (int i = 0; i < parameters.length; i = i + 2) {
            result.put((E) parameters[i], parameters[i + 1]);
        }
        return result;
    }

    public static <T> T checkNotNull(T o) {
        if (o == null) {
            throw LOGGER.getIllegalStateException(o.getClass() + " must not be null");
        }
        return o;
    }

    public static String format(String state, Object... params) {
        return (params.length == 0) ? state : String.format(state, params);
    }
}
